<?php
namespace Tbf\Bundle\RouteBundle\Loader;
use Symfony\Component\Yaml\Yaml;
use Tbf\Bundle\RouteBundle\Matcher\DirMapMatcher;
use Tbf\Bundle\RouteBundle\Exclude\PathInfoExclude;
use Tbf\Bundle\RouteBundle\Matcher\FileMapMatcher;
use Tbf\Bundle\RouteBundle\Matcher\RedirectMatcher;
use Tbf\Bundle\RouteBundle\RouteConfig;

class ArrayLoader implements LoaderInterface{
    protected $config_array;
    function __construct($config_array){
        $this->config_array = $config_array;
    }
    function getConfig(){
        $config_type_map = array(
            'exclude_url'=>1,
            'dir_map'=>1,
            'file_map'=>1,
            'redirect_url'=>1,
        );
        $config = new RouteConfig();
        foreach($this->config_array as $type=>$v1){
            if (empty($config_type_map[$type])){
                throw new \Exception('config type of route not exist');
            }
            call_user_func(array($this,'load_'.$type),$config,$v1);
        }
        return $config;
    }
    function load_exclude_url(RouteConfig $config,$data){
        foreach($data as $v2){
            $config->addExcludeRoute(new PathInfoExclude($v2));
        }
    }
    function load_dir_map(RouteConfig $config,$data){
        foreach($data as $key=>$value){
            $config->addMatchRoute(new DirMapMatcher($key,$value));
        }
    }
    function load_file_map(RouteConfig $config,$data){
        foreach($data as $key=>$value){
            $config->addMatchRoute(new FileMapMatcher($key,$value));
        }
    }
    function load_redirect_url(RouteConfig $config,$data){
        foreach($data as $key=>$value){
            $config->addMatchRoute(new RedirectMatcher($key,$value));
        }
    }
}